class FishpondUI extends UIBase{
		private number_group:eui.Group;
		private bg_group:eui.Group;
		private main_group:eui.Group;
		private img_gameBg:eui.Image;

		public  lb_water:eui.Label;
		private lb_hookNum:eui.Label;

		public fishhook:FishHook;
	
		private down_meter:number;
		//下降速度
		private down_speed:number = 50;
		//上升速度
		private up_speed:number = 0.5;
		//是否处于下降完成状态
		public isDown:boolean = false;
		//可钓数目
		private canGetNum:number;

		private fishList:Array<Fish> = [];

		//是否可以钓鱼了
		public isCanCatch = false;

		//鱼钩是否到达指定位置了
		public isArivePos = false;

		//上升过程中是否可以移动背景
		private isCanMoveBg = true;

		//30像素算一米
		public  transformValue = 30;
		private MAX_METERS:number;
		public  hadDownMeter:number;
		private touchX: number;//开始坐标X

		//已经钓到的鱼的数目
		private havCatchNum :number = 0;

		//鱼钩碰撞器
		private FishHookCollider:egret.Rectangle=null;

		private tween:egret.Tween;

		private static _instance:FishpondUI;

		private stoneList:eui.Image[] = [];

    	public constructor() {
			super('FishpondView');
		}

		public init(){
			this.addStone();
		}

		public static instance()
		{
			if(!this._instance)
			{
				this._instance = new FishpondUI();
			}
			return this._instance;
		}

		public InitGame()
		{
			//下降速度
			this.down_speed = 50;
			//上升速度
			this.up_speed = 0.5;
			this.hadDownMeter = 0;
			this.havCatchNum = 0;
			this.isDown = false;
			this.isArivePos = false;
			this.isCanMoveBg = true;
			this.isCanCatch = false;
			this.fishList = [];
			this.down_meter = RoleCache.instance().getFishDepth();
			this.canGetNum = RoleCache.instance().getFishNumber();
			this.touchX = null;
			
			this.fishhook.x = (this.width - this.fishhook.width >> 1) - (this.fishhook.width >> 1);
			this.fishhook.y = 0;
			this.lb_hookNum.text = this.havCatchNum+'/'+this.canGetNum;
			this.fishhook.setZeroPoint(this.fishhook.x + this.fishhook.width -16, this.fishhook.y);
			egret.Tween.removeTweens(this.bg_group);
			egret.Tween.removeTweens(this.fishhook);
			this.addFish();
			this.addEvent();
			this.main_group.visible = true;
		}

		protected addEvent(){
			GameUtil.addEventListener(EventConst.CatchStart,this.catchStart, this);
		}

		//可以触碰
		protected catchStart()
		{
			this.isArivePos = true;
			
			if(GuideCache.instance().getGuideStatusByIdAndSubId(1,2))
			{
				//经历过第二次引导能移动
				this.main_group.addEventListener( egret.TouchEvent.TOUCH_BEGIN, this.onClickBegin, this);
				this.main_group.addEventListener( egret.TouchEvent.TOUCH_MOVE, this.moveFunc, this);
			}
			
			// this.setFishHookCollider();

			let leftMeter = this.down_meter - this.hadDownMeter;
			let lefty = leftMeter*this.transformValue;
			let time = lefty/this.down_speed; 
			this.tween = egret.Tween.get(this.bg_group, { onChange: this.bgChange, onChangeObj: this });
			this.tween.to({
				y:-lefty
			}, (time/60) * 1000, egret.Ease.sineOut)
			.call(()=>{
				//到了可以钓鱼的位置
				if(GuideCache.instance().getGuideStatusByIdAndSubId(1,2))
				this.isCanCatch = true;
			})
			.wait(500)
			.call(()=>{
				//往回走
				if(!GuideCache.instance().getGuideStatusByIdAndSubId(1,2))
				{	
					let data = {
						curGuideId : 1,
						curGuideSubId : 2
					}
					this.main_group.addEventListener( egret.TouchEvent.TOUCH_BEGIN, this.onClickBegin, this);
					this.main_group.addEventListener( egret.TouchEvent.TOUCH_MOVE, this.moveFunc, this);
					GuideManager.instance().showGuide(data);
				}
				else
				{
					this.goback();
				}
			})
		}

		public goback()
		{
			this.tween = null;
			this.tween = egret.Tween.get(this.bg_group, { onChange: this.bgChange, onChangeObj: this });
			let lefty = this.bg_group.y;
			let upTime = (-lefty)/this.up_speed;
			this.tween.to({
				y:0
			}, upTime)
			.call(()=>{
				//到了收钩子的位置
				this.BackHook();
			});
		}

		//设置鱼钩的世界坐标转局部坐标
		private setFishHookCollider()
		{
			let worldPoint:egret.Point =  this.main_group.localToGlobal(this.fishhook.x, this.fishhook.y);
			let localPoint:egret.Point = this.bg_group.globalToLocal(worldPoint.x, worldPoint.y);
			if(!this.FishHookCollider)
			{
				this.FishHookCollider = egret.Rectangle.create();
			}
			this.FishHookCollider.setTo(localPoint.x, localPoint.y+this.fishhook.height/2, this.fishhook.width-20, this.fishhook.height/2);
		}

		//鱼群游动
		public fishMove()
		{
			this.checkHit();
			let deltY = this.bg_group.y;
			let deltY2 = Math.abs(this.bg_group.y) + this.main_group.height;
			
			for(let v of this.fishList)
			{
				if(v.y < Math.abs(deltY) || v.y > deltY2)
				{
					v.visible = false;
				}
				else
				{
					v.visible = true;
					let dir = v.dir == DirType.left?-1:1;
					v.x += dir * v.getSpeed();
					if(v.x < -200 || v.x > this.width)
					{
						v.changeFishDir();
					}
				}
			}
			return true;
		}

		//检测碰撞
		public checkHit()
		{
			if(!this.isCanCatch) return;
			this.setFishHookCollider();
			for (var i: number = 0; i < this.fishList.length; i++)
			{
				if(this.havCatchNum < this.canGetNum)
				{
					if(GameUtil.hitRect(this.fishList[i], this.FishHookCollider))
					{
						this.fishhook.catchFish(this.fishList[i]);
						this.setHookNum();
						this.addTips(this.fishList[i].getFishData().price);
						this.fishList.splice(i, 1);
					}
				}
				else
				{
					this.isCanCatch = false;
					this.removeEvent();
					let upSpeed = 5;
					let time = Math.floor((this.hadDownMeter*this.transformValue) / upSpeed);
					this.tween = null;
					EffectUtil.instance().StopEffect(this.bg_group);
					egret.setTimeout(()=>{
						egret.Tween.get(this.bg_group).to({y:0},time).call(()=>{
								this.fishhook.goBack2(200);
							})
					},this,500);
					break;
				}
			}
		}

		//结束本轮游戏，清除数据
		public FinishThisGame()
		{
			egret.stopTick(this.fishMove, this);
			this.removeFish();
			this.number_group.visible = false;
			let data = this.fishhook.getFishData();
			GameUtil.dispatchEvent(EventConst.ACCOUNT_EVENT,{data:data});
			this.fishhook.clear();
			this.main_group.visible = false;
		}

		protected setHookNum()
		{
			this.havCatchNum ++;
			this.lb_hookNum.text = this.havCatchNum+'/'+this.canGetNum;
		}

		//背景移动
		public moveBg(){
			this.number_group.visible = true;
			//帧事件做法1
			// this.addEventListener(egret.Event.ENTER_FRAME, this.update,this);
			this.fishhook.MoveDown();
		}

		protected onClickBegin(evt:egret.TouchEvent):void
		{
			this.touchX = evt.stageX;
		}

		protected moveFunc(evt:egret.TouchEvent)
		{
			if(!this.touchX)
			{
				this.touchX = evt.stageX;
			}
			let deltX:number = evt.stageX - this.touchX;
			
			this.fishhook.x += 1.3*deltX;
			if(this.fishhook.x - this.fishhook.width > this.width)
			{
				this.fishhook.x = this.width - this.fishhook.width;
			}
			if(this.fishhook.x < 0)
			{
				this.fishhook.x = 0;
			}
			egret.Tween.get(this.fishhook).wait(60).call(()=>{
				this.fishhook.zeroX += 1.3*deltX;
				if(this.fishhook.zeroX<0)
				{
					this.fishhook.zeroX = 0;
				}
				if(this.fishhook.zeroX>(this.width - this.fishhook.width))
				{
					this.fishhook.zeroX = this.width - this.fishhook.width;
				}
			});
			this.fishhook.changeline();

			this.touchX = evt.stageX;
		}

		//添加石头随机
		protected addStone()
		{	
			let self = this;
			let addOneStone = function(type)
			{
				//0左边 1右边
				let stone = new eui.Image();
				if(type == 0)
				{
					stone.source = 'stone_right_png';
					stone.x = 0;
				}
				else
				{
					let texture = RES.getRes('stone_left_png');
					stone.source = texture;
					stone.x = self.width - texture.textureWidth;
				}
				self.bg_group.addChild(stone);
				return stone;
			}

			for(let i=120;i<this.img_gameBg.height;i+=1200)
			{
				let type = Math.ceil(Math.random() * 3 << 0);
				if(type == 0)
				{
					//添加左边石头
					let stone = addOneStone(0);
					stone.y = i;
				}
				else if(type == 1)
				{
					//添加右边石头
					let stone = addOneStone(1);
					stone.y = i;
				}
				else
				{
					let dir = Math.random() >= 0.5 ? 1:-1;
					//添加两边石头
					let right_stone = addOneStone(1);
					let left_stone = addOneStone(0);
					right_stone.y = i+Math.random() * 150 << 0;
					left_stone.y = i-Math.random() * 150 << 0;
				}
			}
		}

		//通过深度添加鱼群
		public addFish()
		{
			let data = FishPondTpl.instance().getDataByDepth(this.down_meter+40);
			for(let v of data)
			{
				let depth = v.depth;
				let depthArr = depth.split('_');
				let startY = this.transformValue * parseInt(depthArr[0]);
				let endY = this.transformValue * parseInt(depthArr[1]);
				for(let i=1;i<=v.num;i++)
				{
					let id = GameUtil.RandomByWeight(v.probability);
					id = id == -1?1:id;
					let fish = new Fish(id);
					this.fishList.push(fish);
					//多加15像素看起来更舒服
					fish.y = Math.random() * (endY - startY) + startY + 15;
					if(fish.dir == DirType.left)
					{
						fish.x = Math.random() * (this.width+GameConst.MaxWidth);
					}
					else
					{
						fish.x = Math.random() * (this.width+GameConst.MaxWidth) - GameConst.MaxWidth;
					}
					fish.visible = false;
					this.bg_group.addChild(fish);
				}
			}
			egret.startTick(this.fishMove, this);
		}

		//背景改变的函数
		public bgChange()
		{	
			this.hadDownMeter = Math.floor((-this.bg_group.y)/this.transformValue);
			this.lb_water.text = this.hadDownMeter+'m';
		}

		//增加
		public addTips(price:number)
		{
			let str = '+$'+price;
			let tips = TipsPool.createTips(str,this,1);
			if(tips)
			{
				tips.x = this.width >> 1;
				tips.y = this.height >> 1;
			}
			WeChatgGameManager.instance().vibrateShort();
		}

		//收回钩子
		protected BackHook()
		{
			this.isCanCatch = false;
			this.tween = null;
			EffectUtil.instance().StopEffect(this.bg_group);
			this.removeEvent();
			egret.setTimeout(()=>{
				this.fishhook.goBack2(200);
			},this, 200)
		}

		protected removeEvent(){
			// this.removeEventListener(egret.Event.ENTER_FRAME, this.update,this);
			this.main_group.removeEventListener( egret.TouchEvent.TOUCH_BEGIN, this.onClickBegin, this);
			this.main_group.removeEventListener( egret.TouchEvent.TOUCH_MOVE, this.moveFunc, this );
			GameUtil.removeEventListener(EventConst.CatchStart,this.catchStart);
			// egret.stopTick(this.fishMove,this);
		}

		protected clear(){
			super.clear();
			this.removeFish();
			this.fishhook.destory();
			this.fishhook = null;
			this.fishList = null;
			this.tween = null;
			this.FishHookCollider = null;
		}

		public removeFish()
		{
			egret.stopTick(this.fishMove,this);
			for(let v of this.fishList)
			{
				v.destory();
			}
			this.fishList = [];
		}
}